﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Fish.Models;
using Fish.DAL;

namespace Fish.Controllers
{
    /** 
     * This class is responsible for creating, displaying, and editing wall posts.
     */
    [Authorize]
    public class WallPostsController : Controller
    {
        private FishContext db = new FishContext();

        // GET: /WallPosts/
        public ActionResult Index()
        {

            if (Session["eventID"] != null)
            {
                System.Diagnostics.Debug.WriteLine("EVENT: " + Session["eventID"]);
                int eventID = int.Parse(Session["eventID"].ToString());
                var eventPosts = db.WallPosts.Where(e => e.Event.ID == eventID);
                return View(eventPosts.ToList());
            }
            else
                return RedirectToAction("Expired", "Error");
        }

        // GET: /WallPosts/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("PageNotFound", "Error");

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            WallPosts wallposts = db.WallPosts.Find(id);
            if (wallposts == null)
            {
                return RedirectToAction("Expired", "Error");

                return HttpNotFound();
            }
            return View(wallposts);
        }

        // GET: /WallPosts/Create
        public ActionResult Create()
        {
            if (Session["eventID"] != null)
            {
                return View();
            }
            else
                return RedirectToAction("Expired", "Error");
        }

        // POST: /WallPosts/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Text,Created,Modified")] WallPosts wallposts)
        {
            if (ModelState.IsValid)
            {
                int eventID = int.Parse(Session["eventID"].ToString());
                int userID = int.Parse(Session["userID"].ToString());
                Events eventUSed = db.Events.Find(eventID);
                Users user = db.Users.Find(userID);

                wallposts.Event = eventUSed;
                wallposts.User = user;
                db.WallPosts.Add(wallposts);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(wallposts);
        }

        // GET: /WallPosts/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("PageNotFound", "Error");

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            WallPosts wallposts = db.WallPosts.Find(id);
            if (wallposts == null)
            {
                return RedirectToAction("Expired", "Error");

                return HttpNotFound();
            }
            return View(wallposts);
        }

        // POST: /WallPosts/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Text,Created,Modified")] WallPosts wallposts)
        {
            if (ModelState.IsValid)
            {
                db.Entry(wallposts).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(wallposts);
        }

        // GET: /WallPosts/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("PageNotFound", "Error");

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            WallPosts wallposts = db.WallPosts.Find(id);
            if (wallposts == null)
            {
                return RedirectToAction("Expired", "Error");

                return HttpNotFound();
            }
            return View(wallposts);
        }

        // POST: /WallPosts/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            WallPosts wallposts = db.WallPosts.Find(id);
            db.WallPosts.Remove(wallposts);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
